#include<QFileDialog>
#include<QMessageBox>
#include <QtCore/QTextStream>

#include "pt.h"
#include "ui_pt.h"
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include <sys\stat.h>
#include <iostream>
#include <string>
#include<assert.h>

//somechanges

using namespace std;

QString bmppath;
QString textpath;

unsigned long seek_pos(FILE *fp,int p)
{
      int i=0;
      unsigned long pos=0;
      unsigned char *bmp_buf,temp;
      bmp_buf=&temp;
      while(i<4)
      {
       fseek(fp,i+p,SEEK_SET);
       fread(bmp_buf,1,1,fp);
       pos+=*bmp_buf*(pow(256,i));
       i++;
     }
     return pos;
}

/*void replace(char * path)
{

    string str =  path ;
    string searchString( "\\" );
    string replaceString( "\\\\" );

    assert( searchString != replaceString );

    string::size_type pos = 0;
    while ( (pos = str.find(searchString, pos)) != string::npos ) {
        str.replace( pos, searchString.size(), replaceString );
        pos++;
    }
    //cout << str << endl;
} */


pt::pt(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::pt)
{
    ui->setupUi(this);

    connect( ui->pushButton_browse, SIGNAL( clicked() ), this, SLOT( browse() ) );
    connect( ui->pushButton_browse_2, SIGNAL( clicked() ), this, SLOT( browse_2() ) );
    connect( ui->pushButton_encode, SIGNAL( clicked() ), this, SLOT( encode() ) );


}

pt::~pt()
{
    delete ui;
}

void pt::browse()
{

    textpath = QFileDialog::getOpenFileName(
        this,
        "Choose a file to open",
        QString::null,
        QString::null);

    ui->lineEdit->setText( textpath );

}

void pt::browse_2()
{

    bmppath = QFileDialog::getOpenFileName(
        this,
        "Choose a file to open",
        QString::null,
        QString::null);

    ui->lineEdit_2->setText( bmppath );

}

void pt::encode()
{

    unsigned long pos,x_res,y_res;
       unsigned char *bmp_buf,*txt_buf,temp1,temp,temp2,i=0;
       struct stat file_stat;
       int tp;
       bmp_buf=&temp;
       txt_buf=&temp1;

       char *textfile;
       textfile = textpath.toLatin1().data();
       char *bmpfile;
       bmpfile = bmppath.toLatin1().data();

       //replace(textfile);
      // replace(bmpfile);
       FILE *fpt=fopen(textfile,"r");
       FILE *fp=fopen(bmpfile,"rb+");




      // FILE *fpt=fopen("c:/test.txt","r");
    //  FILE *fp=fopen("c:/test.bmp","rb+");

             x_res=seek_pos(fp,18);
             y_res=seek_pos(fp,22);
             tp=(int)(x_res*y_res*3/8);
   //          if (stat(textfile, &file_stat) ==0)
   //          if(file_stat.st_size>tp)
   //            {
   //              ui->msg->setText("Error: Message in Text File is too long !!");
   //            }
             pos=seek_pos(fp,10);

             fseek(fp,pos,SEEK_SET);
             while(fread(txt_buf,1,1,fpt)>0)
             {
                 i=0;
                 while(i<8)
                 {
                  temp2=*txt_buf&1;
                  fread(bmp_buf,1,1,fp);
                  *bmp_buf=*bmp_buf&0xFE|temp2;
                  fseek(fp,pos,SEEK_SET);
                  fwrite(bmp_buf,1,1,fp);
                  fseek(fp,++pos,SEEK_SET);
                  *txt_buf=*txt_buf>>1;
                  i++;

                 }
             }

             //end of file denoted  by zeroing the lsb of 8 bytes.
             i=0;
             while(i<8)
             {
             fread(bmp_buf,1,1,fp);
             *bmp_buf=*bmp_buf&0xFE;
             fseek(fp,pos++,SEEK_SET);
             fwrite(bmp_buf,1,1,fp);
             i++;
             }
             fclose(fp);
             fclose(fpt);


             ui->msg->setText(textfile);


       QString filename;
       filename = ui->lineEdit->text();

   //    QFile inputFile(filename);
   //    inputFile.open(QIODevice::ReadWrite);
   //
   //
   //    QDataStream out(&inputFile);
   //                out.setVersion(QDataStream::Qt_4_5);
   //                out <<"Aamir";
   //
   //    inputFile.close();







}

void pt::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
